#coding=utf-8
from uliweb import expose
from sqlalchemy import *
from uliweb.orm import *
from sqlalchemy.sql import select,func
from sqlalchemy.sql import and_, or_, not_, outerjoin
import datetime
from datetime import date
from uliweb import decorators

def __begin__():
    from uliweb import function
    return function('require_login')()

@expose('/charts')
def charts():
    return {}

#风险整改报表-风险整改措施完成情况统计
@expose('/charts/risksolution/status/counting')
def charts_risksolution_status_counting():
    from uliweb.orm import get_model
    from uliweb.utils.generic import SimpleListView
    from uliweb import request
#    from query import create_manage_riskpoint_executing_query
    from uliweb.core.html import Tag
    from decimal import Decimal
    from datetime import date
    
    Risksolution = get_model('risksolution')
    Tasks_Develop = get_model('tasks_develop')
    Requirement = get_model('requirement')
    Riskpoint = get_model('riskpoint')
    Kfzx = get_model('kfzx')
#    query = create_manage_riskpoint_executing_query(url_for(charts_riskpoint_current_executing))
#    c = query.run()
#    query = None
#    condition = None
#    if (request.user.is_gm)and(request.user.department=='2'):
#        c['kfzx'] = request.user._kfzx_
    
    #生成表头
    header = []
    
    header.append({'name':'kfzx','verbose_name':u'开发中心(主责方)', 'width':120})
    header.append({'name':'high_finish','verbose_name':u'高/已完成', 'width':50})    
    header.append({'name':'high_nonfinish','verbose_name':u'高/未完成', 'width':50})    
    header.append({'name':'level_finish','verbose_name':u'中/已完成', 'width':50})    
    header.append({'name':'level_nonfinish','verbose_name':u'中/未完成', 'width':50})    
    header.append({'name':'low_finish','verbose_name':u'低/已完成', 'width':50})    
    header.append({'name':'low_nonfinish','verbose_name':u'低/未完成', 'width':50})    
    header.append({'name':'emergency_finish','verbose_name':u'紧急/已完成', 'width':50})    
    header.append({'name':'emergency_nonfinish','verbose_name':u'紧急/未完成', 'width':50})    
    header.append({'name':'general_finish','verbose_name':u'一般/已完成', 'width':50})    
    header.append({'name':'general_nonfinish','verbose_name':u'一般/未完成', 'width':50})    
    
   
    data_tmp={}

    for row in Kfzx.filter(and_(Kfzx.c.type == '1')).order_by(Kfzx.c.id):
        kfzx_id = row.id
        data_tmp[kfzx_id] = row.name
        data_tmp[kfzx_id] = {}
        data_tmp[kfzx_id]['kfzx'] = row.name
        data_tmp[kfzx_id]['high_finish'] = 0
        data_tmp[kfzx_id]['high_nonfinish'] = 0
        data_tmp[kfzx_id]['level_finish'] = 0
        data_tmp[kfzx_id]['level_nonfinish'] = 0
        data_tmp[kfzx_id]['low_finish'] = 0
        data_tmp[kfzx_id]['low_nonfinish'] = 0            
        data_tmp[kfzx_id]['emergency_finish'] = 0            
        data_tmp[kfzx_id]['emergency_nonfinish'] = 0
        data_tmp[kfzx_id]['general_finish'] = 0
        data_tmp[kfzx_id]['general_nonfinish'] = 0

    #以风险点风险级别和主责方为维度，统计整改措施为已完成的记录数
#    sel = select([Risksolution.c.tech_lead_depart,Riskpoint.c.level,func.count(func.distinct(Risksolution.c.solution_id))],
#                or_(and_(
#                    Risksolution.c.solution_mode == '1',
#                    Risksolution.c.task_id != None,
#                    Tasks_Develop.c.id == Risksolution.c.task_id,
#                    Tasks_Develop.c.stage == 'FN',
#                    Riskpoint.c.point_id == Risksolution.c.riskpoint),
#                    and_(Risksolution.c.solution_mode == '2',
#                        Risksolution.c.nondev_riskpoint_status == 'F',
#                        Riskpoint.c.point_id == Risksolution.c.riskpoint)
#                     )).group_by(Risksolution.c.tech_lead_depart,Riskpoint.c.level)
    
    sel = select([Risksolution.c.tech_lead_depart,Riskpoint.c.level,func.count(func.distinct(Risksolution.c.solution_id))],
                and_(Riskpoint.c.point_id == Risksolution.c.riskpoint,
                            or_(and_(
                                    Risksolution.c.solution_mode == '1',
                                    Risksolution.c.task_id != None,
                                    Risksolution.c.nondev_riskpoint_status == 'F'),
                                and_(Risksolution.c.solution_mode == '2',
                                    Risksolution.c.nondev_riskpoint_status == 'F'))
                     )).group_by(Risksolution.c.tech_lead_depart,Riskpoint.c.level)
    
    sel_rs = sel.execute()
#    print '+++++++',sel
    for row in sel_rs:
        kfzx,level,num = row
        if not num or not kfzx:
            continue
        
#        kfzx = str(kfzx)
        if level and level == '1' and kfzx and (kfzx in data_tmp):
            data_tmp[kfzx]['high_finish'] = num
        if level and level == '2' and kfzx and (kfzx in data_tmp):
            data_tmp[kfzx]['level_finish'] =num
        if level and level == '3' and kfzx and (kfzx in data_tmp):
            data_tmp[kfzx]['low_finish'] =num
    
    #以风险点风险级别和主责方为维度，统计整改措施为未完成的记录数
#    sel = select([Risksolution.c.tech_lead_depart,Riskpoint.c.level,func.count(func.distinct(Risksolution.c.solution_id))],
#                or_(and_(
#                     Risksolution.c.solution_mode == '1',
#                     or_(and_(Risksolution.c.task_id != None,
#                            Tasks_Develop.c.id == Risksolution.c.task_id,
#                            Tasks_Develop.c.stage != 'FN'),
#                         and_(Risksolution.c.task_id == None)),
#                     Riskpoint.c.point_id == Risksolution.c.riskpoint),
#                    and_(Risksolution.c.solution_mode == '2',
#                        Risksolution.c.nondev_riskpoint_status != 'F',
#                        Riskpoint.c.point_id == Risksolution.c.riskpoint)
#                     )).group_by(Risksolution.c.tech_lead_depart,Riskpoint.c.level)
    
    sel = select([Risksolution.c.tech_lead_depart,Riskpoint.c.level,func.count(func.distinct(Risksolution.c.solution_id))],
                and_(Riskpoint.c.point_id == Risksolution.c.riskpoint,
                            or_(and_(Risksolution.c.solution_mode == '1',

                                or_(and_(Risksolution.c.task_id != None,
                                         Risksolution.c.nondev_riskpoint_status != 'F'),
                                    Risksolution.c.task_id == None)),
                                and_(Risksolution.c.solution_mode == '2',
                                    or_(and_(Risksolution.c.nondev_riskpoint_status!=None,
                                            Risksolution.c.nondev_riskpoint_status != 'F'),
                                        Risksolution.c.nondev_riskpoint_status==None))
                     ))).group_by(Risksolution.c.tech_lead_depart,Riskpoint.c.level)
    
    sel_rs = sel.execute()
#    print '_---------',sel
    for row in sel_rs:
        kfzx,level,num = row
        if not num or not kfzx:
            continue
#        kfzx = str(kfzx)

        if level and level == '1' and kfzx and (kfzx in data_tmp):
            data_tmp[kfzx]['high_nonfinish'] = num
        if level and level == '2' and kfzx and (kfzx in data_tmp):
            data_tmp[kfzx]['level_nonfinish'] =num
        if level and level == '3' and kfzx and (kfzx in data_tmp):
            data_tmp[kfzx]['low_nonfinish'] =num
    #以整改措施紧迫程度和主责方为维度，统计整改措施为已完成的记录数
    sel = select([Risksolution.c.tech_lead_depart,Risksolution.c.urgency,func.count(func.distinct(Risksolution.c.solution_id))],
                or_(and_(
                     Risksolution.c.solution_mode == '1',
                    Risksolution.c.task_id != None,
                    Risksolution.c.nondev_riskpoint_status == 'F'
                    ),
                    and_(Risksolution.c.solution_mode == '2',
                        Risksolution.c.nondev_riskpoint_status == 'F')
                     )).group_by(Risksolution.c.tech_lead_depart,Risksolution.c.urgency)
    
#    sel = select([Risksolution.c.tech_lead_depart,Risksolution.c.urgency,func.count(func.distinct(Risksolution.c.solution_id))],
#                and_(Riskpoint.c.point_id == Risksolution.c.riskpoint,
#                     Risksolution.c.solution_id.in_(
#                        select([Risksolution.c.solution_id],
#                            or_(and_(
#                                    Risksolution.c.solution_mode == '1',
#                                    Risksolution.c.task_id != None,
#                                    Tasks_Develop.c.id == Risksolution.c.task_id,
#                                    Tasks_Develop.c.stage == 'FN'),
#                                and_(Risksolution.c.solution_mode == '2',
#                                    Risksolution.c.nondev_riskpoint_status == 'F'))
#                                )
#                     ))).group_by(Risksolution.c.tech_lead_depart,Risksolution.c.urgency)
    
    sel_rs = sel.execute()
    for row in sel_rs:
        kfzx,urgency,num = row
        if not num or not kfzx:
            continue
        
#        kfzx = str(kfzx)
        if urgency and (urgency == '1' or urgency == '2') and kfzx and (kfzx in data_tmp):#紧迫程度为紧急或特急
            data_tmp[kfzx]['emergency_finish'] = data_tmp[kfzx]['emergency_finish'] + num
        if urgency and urgency == '3' and kfzx and (kfzx in data_tmp):#紧迫程度为一般
            data_tmp[kfzx]['general_finish'] +=num
    #以整改措施紧迫程度和主责方为维度，统计整改措施为未完成的记录数
    sel = select([Risksolution.c.tech_lead_depart,Risksolution.c.urgency,func.count(func.distinct(Risksolution.c.solution_id))],
                or_(and_(
                     Risksolution.c.solution_mode == '1',
                    or_(and_(Risksolution.c.task_id != None,
                           Risksolution.c.nondev_riskpoint_status != 'F'),
                        and_(Risksolution.c.task_id == None))),
                    and_(Risksolution.c.solution_mode == '2',
                        or_(and_(Risksolution.c.nondev_riskpoint_status!=None,
                                Risksolution.c.nondev_riskpoint_status != 'F'),
                            Risksolution.c.nondev_riskpoint_status==None))                    
                     )).group_by(Risksolution.c.tech_lead_depart,Risksolution.c.urgency)
    sel_rs = sel.execute()
    for row in sel_rs:
        kfzx,urgency,num = row
        if not num or not kfzx:
            continue
        
#        kfzx = str(kfzx)
        if urgency and (urgency == '1' or urgency == '2') and kfzx and (kfzx in data_tmp):#紧迫程度为紧急或特急
            data_tmp[kfzx]['emergency_nonfinish'] = data_tmp[kfzx]['emergency_nonfinish'] + num
        if urgency and urgency == '3' and kfzx and (kfzx in data_tmp):#紧迫程度为一般
            data_tmp[kfzx]['general_nonfinish'] +=num
    
    data = []
    for l,v in data_tmp.items():
        data.append(v)
    
    pageno = int(request.values.get('page', 1)) - 1
    rows_per_page = int(request.values.get('rows', settings.get_var('PARA/ROWS_PER_PAGE', 10)))
#    print '+++',data
    #展示        
    view =  SimpleListView(header, data, id='riskpoint_count_table',#table_width=True,
        pagination=True, pageno=pageno,
        rows_per_page=rows_per_page)
    
    if 'download' in request.GET:        
#        fields_convert_map = {'weekreport':weekreport_title}
        return view.download('risksolution_status_counting.xls', action='download', timeout=0)
    elif 'data' in request.values:
        return json(view.json())
    else:
        result = view.run(head=True, body=True)
        result['table'] = view
        return result
    
    
#风险整改报表-风险级别与整改紧迫程度统计表
@expose('/charts/risksolution/level/urgency')
def charts_risksolution_level_urgency():
    from uliweb.orm import get_model
    from uliweb.utils.generic import SimpleListView
    from uliweb import request
#    from query import create_manage_riskpoint_executing_query
    from uliweb.core.html import Tag
    from decimal import Decimal
    from datetime import date
    
    Risksolution = get_model('risksolution')
    Tasks_Develop = get_model('tasks_develop')
    Requirement = get_model('requirement')
    Riskpoint = get_model('riskpoint')
    Kfzx = get_model('kfzx')
#    query = create_manage_riskpoint_executing_query(url_for(charts_riskpoint_current_executing))
#    c = query.run()
#    query = None
#    condition = None
#    if (request.user.is_gm)and(request.user.department=='2'):
#        c['kfzx'] = request.user._kfzx_
    
    #生成表头
    header = []
    
    header.append({'name':'kfzx','verbose_name':u'开发中心(主责方)', 'width':120})
    header.append({'name':'risk_level','verbose_name':u'风险级别', 'width':50})    
    header.append({'name':'emergency_total','verbose_name':u'紧急/总数', 'width':50})    
    header.append({'name':'emergency_finish','verbose_name':u'紧急/已解决', 'width':50})    
    header.append({'name':'nonemergency_total','verbose_name':u'非紧急/总数', 'width':50})    
    header.append({'name':'nonemergency_finish','verbose_name':u'非紧急/已解决', 'width':50})    
    header.append({'name':'total_num','verbose_name':u'合计/总数', 'width':50})    
    header.append({'name':'total_finish','verbose_name':u'合计/已解决', 'width':50})    
    
   
    data_tmp={}

    for row in Kfzx.filter(and_(Kfzx.c.type == '1')).order_by(Kfzx.c.id):
        kfzx_id = row.id
        data_tmp[kfzx_id] = row.name
        data_tmp[kfzx_id] = {}
        data_tmp[kfzx_id]['1'] ={}
        data_tmp[kfzx_id]['1']['kfzx'] = row.name
        data_tmp[kfzx_id]['1']['risk_level'] = ''
        data_tmp[kfzx_id]['1']['emergency_total'] = 0
        data_tmp[kfzx_id]['1']['emergency_finish'] = 0
        data_tmp[kfzx_id]['1']['nonemergency_total'] = 0
        data_tmp[kfzx_id]['1']['nonemergency_finish'] = 0
        data_tmp[kfzx_id]['1']['total_num'] = 0            
        data_tmp[kfzx_id]['1']['total_finish'] = 0            
        data_tmp[kfzx_id]['2'] ={}
        data_tmp[kfzx_id]['2']['kfzx'] = row.name
        data_tmp[kfzx_id]['2']['risk_level'] = ''
        data_tmp[kfzx_id]['2']['emergency_total'] = 0
        data_tmp[kfzx_id]['2']['emergency_finish'] = 0
        data_tmp[kfzx_id]['2']['nonemergency_total'] = 0
        data_tmp[kfzx_id]['2']['nonemergency_finish'] = 0
        data_tmp[kfzx_id]['2']['total_num'] = 0            
        data_tmp[kfzx_id]['2']['total_finish'] = 0
        
    #以风险点风险级别、主责方和紧迫程度为维度，统计整改措施为已完成的记录数
    sel = select([func.count(func.distinct(Risksolution.c.solution_id)),Risksolution.c.tech_lead_depart,Riskpoint.c.level,
                  Risksolution.c.urgency],
                or_(and_(
                        Risksolution.c.solution_mode == '1',
                        Risksolution.c.task_id != None,
                        Risksolution.c.nondev_riskpoint_status == 'F',                  
                        Riskpoint.c.point_id == Risksolution.c.riskpoint),
                    and_(Risksolution.c.solution_mode == '2',
                        Risksolution.c.nondev_riskpoint_status == 'F',
                        Riskpoint.c.point_id == Risksolution.c.riskpoint)
                     )).group_by(Risksolution.c.tech_lead_depart,Riskpoint.c.level,Risksolution.c.urgency).order_by(Risksolution.c.tech_lead_depart)
    sel_rs = sel.execute()
    for row in sel_rs:
        num,kfzx,level,urgency = row
        if not num or not kfzx:
            continue
        
        if urgency and (urgency == '1' or urgency == '2') and kfzx and (kfzx in data_tmp):
            if level and level == '1':
                data_tmp[kfzx]['1']['risk_level'] = u'高危'
                data_tmp[kfzx]['1']['emergency_finish'] += num
                data_tmp[kfzx]['1']['emergency_total'] += num
                data_tmp[kfzx]['1']['total_num'] += num
                data_tmp[kfzx]['1']['total_finish'] += num
            elif level and (level == '2' or level == '3'):
                data_tmp[kfzx]['2']['risk_level'] = u'非高危(中+低)'
                data_tmp[kfzx]['2']['emergency_finish'] += num
                data_tmp[kfzx]['2']['emergency_total'] += num
                data_tmp[kfzx]['2']['total_num'] += num
                data_tmp[kfzx]['2']['total_finish'] += num
            
        if urgency and urgency == '3' and kfzx and (kfzx in data_tmp):
            if level and level == '1':
                data_tmp[kfzx]['1']['risk_level'] = u'高危'
                data_tmp[kfzx]['1']['nonemergency_finish'] += num
                data_tmp[kfzx]['1']['nonemergency_total'] += num
                data_tmp[kfzx]['1']['total_num'] += num
                data_tmp[kfzx]['1']['total_finish'] += num
            elif level and (level == '2' or level == '3'):
                data_tmp[kfzx]['2']['risk_level'] = u'非高危(中+低)'
                data_tmp[kfzx]['2']['nonemergency_finish'] += num
                data_tmp[kfzx]['2']['nonemergency_total'] += num
                data_tmp[kfzx]['2']['total_num'] += num
                data_tmp[kfzx]['2']['total_finish'] += num

    
    #以风险点风险级别、主责方和紧迫程度为维度，统计整改措施为未完成的记录数
    sel = select([func.count(func.distinct(Risksolution.c.solution_id)),Risksolution.c.tech_lead_depart,Riskpoint.c.level,
                  Risksolution.c.urgency],
                or_(and_(
                     Risksolution.c.solution_mode == '1',
                    or_(and_(Risksolution.c.task_id != None,
                           Risksolution.c.nondev_riskpoint_status != 'F'),
                        and_(Risksolution.c.task_id == None)),
                    Riskpoint.c.point_id == Risksolution.c.riskpoint),
                    and_(Risksolution.c.solution_mode == '2',
                        or_(and_(Risksolution.c.nondev_riskpoint_status != None,
                                Risksolution.c.nondev_riskpoint_status != 'F'),
                            Risksolution.c.nondev_riskpoint_status == None),
                        Riskpoint.c.point_id == Risksolution.c.riskpoint)
                     )).group_by(Risksolution.c.tech_lead_depart,Riskpoint.c.level,Risksolution.c.urgency).order_by(Risksolution.c.tech_lead_depart)
    sel_rs = sel.execute()
    for row in sel_rs:
        num,kfzx,level,urgency = row
        if not num or not kfzx:
            continue
        
        if urgency and (urgency == '1' or urgency == '2') and kfzx and (kfzx in data_tmp):
            if level and level == '1':
                data_tmp[kfzx]['1']['risk_level'] = u'高危'
#                data_tmp[kfzx]['1']['emergency_finish'] = num
                data_tmp[kfzx]['1']['emergency_total'] += num
                data_tmp[kfzx]['1']['total_num'] += num
#                data_tmp[kfzx_id]['1']['total_finish'] += num
            elif level and (level == '2' or level == '3'):
                data_tmp[kfzx]['2']['risk_level'] = u'非高危(中+低)'
#                data_tmp[kfzx]['2']['emergency_finish'] = num
                data_tmp[kfzx]['2']['emergency_total'] += num
                data_tmp[kfzx]['2']['total_num'] += num
#                data_tmp[kfzx_id]['2']['total_finish'] += num
            
        if urgency and urgency == '3' and kfzx and (kfzx in data_tmp):
            if level and level == '1':
                data_tmp[kfzx]['1']['risk_level'] = u'高危'
#                data_tmp[kfzx]['1']['nonemergency_finish'] = num
                data_tmp[kfzx]['1']['nonemergency_total'] += num
                data_tmp[kfzx]['1']['total_num'] += num
#                data_tmp[kfzx_id]['1']['total_finish'] += num
            elif level and (level == '2' or level == '3'):
                data_tmp[kfzx]['2']['risk_level'] = u'非高危(中+低)'
#                data_tmp[kfzx]['2']['nonemergency_finish'] = num
                data_tmp[kfzx]['2']['nonemergency_total'] += num
                data_tmp[kfzx]['2']['total_num'] += num
#                data_tmp[kfzx_id]['2']['total_finish'] += num
    
    data = []
    for l,v in data_tmp.items():
        for p,q in v.items():
            data.append(q)
    
    pageno = int(request.values.get('page', 1)) - 1
    rows_per_page = int(request.values.get('rows', settings.get_var('PARA/ROWS_PER_PAGE', 10)))
#    print '+++',data
    #展示        
    view =  SimpleListView(header, data, id='riskpoint_count_table',#table_width=True,
        pagination=True, pageno=pageno,
        rows_per_page=rows_per_page)
    
    if 'download' in request.GET:        
#        fields_convert_map = {'weekreport':weekreport_title}
        return view.download('risksolution_level_urgency.xls', action='download', timeout=0)
    elif 'data' in request.values:
        return json(view.json())
    else:
        result = view.run(head=True, body=True)
        result['table'] = view
        return result

#风险整改报表-按策略名称维度统计
@expose('/charts/risksolution/kfzx/policy')
def charts_risksolution_kfzx_policy():
    from uliweb.orm import get_model
    from uliweb.utils.generic import SimpleListView
    from uliweb import request
#    from query import create_manage_riskpoint_executing_query
    from uliweb.core.html import Tag
    from decimal import Decimal
    from datetime import date
    
    Risksolution = get_model('risksolution')
    Tasks_Develop = get_model('tasks_develop')
    Requirement = get_model('requirement')
    Riskpoint = get_model('riskpoint')
    Kfzx = get_model('kfzx')
#    query = create_manage_riskpoint_executing_query(url_for(charts_riskpoint_current_executing))
#    c = query.run()
#    query = None
#    condition = None
#    if (request.user.is_gm)and(request.user.department=='2'):
#        c['kfzx'] = request.user._kfzx_
    
    #生成表头
    header = []
    
    header.append({'name':'kfzx','verbose_name':u'开发中心(主责方)', 'width':120})
    header.append({'name':'risk_policy','verbose_name':u'策略名称', 'width':200})    
    header.append({'name':'risksolution_total','verbose_name':u'总数', 'width':50})    
    header.append({'name':'risksolution_finish','verbose_name':u'已解决数', 'width':50})    
    
   
    data_tmp={}

    for row in Kfzx.filter(and_(Kfzx.c.type == '1')).order_by(Kfzx.c.id):
        kfzx_id = row.id
#        data_tmp[kfzx_id] = row.name
        data_tmp[kfzx_id] = {}
#        print '++++++-----------',data_tmp
#        data_tmp[kfzx_id]['kfzx'] = row.name
#        data_tmp[kfzx_id]['risk_policy'] = ''
#        data_tmp[kfzx_id]['risksolution_total'] = 0
#        data_tmp[kfzx_id]['risksolution_finish'] = 0

        
    #以风险点风险策略名称、主责方为维度，统计整改措施为已完成的记录数
    sel = select([Risksolution.c.tech_lead_depart,Riskpoint.c.policy,
                  func.count(func.distinct(Risksolution.c.solution_id))],
                or_(and_(
                     Risksolution.c.solution_mode == '1',
                    Risksolution.c.task_id != None,
                    Risksolution.c.nondev_riskpoint_status == 'F',
                    Riskpoint.c.point_id == Risksolution.c.riskpoint),
                    and_(Risksolution.c.solution_mode == '2',
                        Risksolution.c.nondev_riskpoint_status == 'F',
                        Riskpoint.c.point_id == Risksolution.c.riskpoint)
                     )).group_by(Risksolution.c.tech_lead_depart,Riskpoint.c.policy).order_by(Risksolution.c.tech_lead_depart)
    sel_rs = sel.execute()
    for row in sel_rs:
        kfzx,policy,num = row
        if not num or not kfzx:
            continue
        
        if kfzx and (kfzx in data_tmp):
#            data_tmp[kfzx] = {}
            data_tmp[kfzx][policy] = {}
            data_tmp[kfzx][policy]['kfzx'] = Kfzx.get(kfzx).name
            data_tmp[kfzx][policy]['risk_policy'] = policy
            data_tmp[kfzx][policy]['risksolution_finish'] = num
            data_tmp[kfzx][policy]['risksolution_total'] = num
    
    #以风险点风险策略名称、主责方为维度，统计整改措施为未完成的记录数
    sel = select([Risksolution.c.tech_lead_depart,Riskpoint.c.policy,
                  func.count(func.distinct(Risksolution.c.solution_id))],
                or_(and_(
                     Risksolution.c.solution_mode == '1',
                    or_(and_(Risksolution.c.task_id != None,
                           Risksolution.c.nondev_riskpoint_status != 'F'),
                        and_(Risksolution.c.task_id == None)),                    
                    Riskpoint.c.point_id == Risksolution.c.riskpoint),
                    and_(Risksolution.c.solution_mode == '2',
                      or_(and_(Risksolution.c.nondev_riskpoint_status != None,
                            Risksolution.c.nondev_riskpoint_status != 'F'),
                            Risksolution.c.nondev_riskpoint_status == None),
                        Riskpoint.c.point_id == Risksolution.c.riskpoint)
                     )).group_by(Risksolution.c.tech_lead_depart,Riskpoint.c.policy).order_by(Risksolution.c.tech_lead_depart)
    sel_rs = sel.execute()
    for row in sel_rs:
        kfzx,policy,num = row
        if not num or not kfzx:
            continue
        
        if kfzx and (kfzx in data_tmp) and (policy in data_tmp[kfzx]):
#            data_tmp[kfzx]['risk_policy'] = policy
            data_tmp[kfzx][policy]['risksolution_total'] += num
        elif kfzx and (kfzx in data_tmp) and not (policy in data_tmp[kfzx]):
            data_tmp[kfzx][policy] = {}
            data_tmp[kfzx][policy]['kfzx'] = Kfzx.get(kfzx).name
            data_tmp[kfzx][policy]['risk_policy'] = policy
            data_tmp[kfzx][policy]['risksolution_finish'] = 0
            data_tmp[kfzx][policy]['risksolution_total'] = num
            
    
    data = []
    for l,v in data_tmp.items():
        for p,q in v.items():
            data.append(q)
    
    pageno = int(request.values.get('page', 1)) - 1
    rows_per_page = int(request.values.get('rows', settings.get_var('PARA/ROWS_PER_PAGE', 10)))
#    print '+++',data
    #展示        
    view =  SimpleListView(header, data, id='riskpoint_count_table',#table_width=True,
        pagination=True, pageno=pageno,
        rows_per_page=rows_per_page)
    
    if 'download' in request.GET:        
#        fields_convert_map = {'weekreport':weekreport_title}
        return view.download('risksolution_kfzx_policy.xls', action='download', timeout=0)
    elif 'data' in request.values:
        return json(view.json())
    else:
        result = view.run(head=True, body=True)
        result['table'] = view
        return result


#风险整改报表-按系统名称维度统计
@expose('/charts/risksolution/kfzx/system')
def charts_risksolution_kfzx_system():
    from uliweb.orm import get_model
    from uliweb.utils.generic import SimpleListView
    from uliweb import request
#    from query import create_manage_riskpoint_executing_query
    from uliweb.core.html import Tag
    from decimal import Decimal
    from datetime import date
    
    Risksolution = get_model('risksolution')
    Tasks_Develop = get_model('tasks_develop')
    Requirement = get_model('requirement')
    Riskpoint = get_model('riskpoint')
    Kfzx = get_model('kfzx')
#    query = create_manage_riskpoint_executing_query(url_for(charts_riskpoint_current_executing))
#    c = query.run()
#    query = None
#    condition = None
#    if (request.user.is_gm)and(request.user.department=='2'):
#        c['kfzx'] = request.user._kfzx_
    
    #生成表头
    header = []
    
    header.append({'name':'kfzx','verbose_name':u'开发中心(主责方)', 'width':120})
    header.append({'name':'risk_sys','verbose_name':u'系统名称', 'width':80})
    header.append({'name':'risksolution_num','verbose_name':u'整改措施数', 'width':80})    
    header.append({'name':'ass_num','verbose_name':u'在整改项目中', 'width':80})    
    header.append({'name':'nonass_num','verbose_name':u'非在整改项目中', 'width':80})    
    header.append({'name':'high-risk_num','verbose_name':u'高危风险', 'width':80})    
    header.append({'name':'emergency_num','verbose_name':u'高危且紧急', 'width':80})    
    header.append({'name':'nonemergency_num','verbose_name':u'高危非紧急', 'width':80})    
    
   
    data_tmp={}

    for row in Kfzx.filter(and_(Kfzx.c.type == '1')).order_by(Kfzx.c.id):
        kfzx_id = row.id
#        data_tmp[kfzx_id] = row.name
        data_tmp[kfzx_id] = {}
#        print '++++++-----------',data_tmp
#        data_tmp[kfzx_id]['kfzx'] = row.name
#        data_tmp[kfzx_id]['risk_policy'] = ''
#        data_tmp[kfzx_id]['risksolution_total'] = 0
#        data_tmp[kfzx_id]['risksolution_finish'] = 0

        
    #按系统名称维度统计，在项目中
    sel = select([Risksolution.c.tech_lead_depart,Riskpoint.c.risk_sys,
                  func.count(func.distinct(Risksolution.c.solution_id))],
                and_(Riskpoint.c.point_id == Risksolution.c.riskpoint,
                    Riskpoint.c.in_project == True)
                     ).group_by(Risksolution.c.tech_lead_depart,Riskpoint.c.risk_sys).order_by(Risksolution.c.tech_lead_depart)
    sel_rs = sel.execute()
    for row in sel_rs:
        kfzx,risk_sys,num = row
        if not num or not kfzx:
            continue
        
        if kfzx and (kfzx in data_tmp):
#            data_tmp[kfzx] = {}
            data_tmp[kfzx][risk_sys] = {}
            data_tmp[kfzx][risk_sys]['kfzx'] = Kfzx.get(kfzx).name
            data_tmp[kfzx][risk_sys]['risk_sys'] = risk_sys
            data_tmp[kfzx][risk_sys]['ass_num'] = num
            data_tmp[kfzx][risk_sys]['nonass_num'] = 0
            data_tmp[kfzx][risk_sys]['risksolution_num'] = num
            data_tmp[kfzx][risk_sys]['high-risk_num'] = 0
            data_tmp[kfzx][risk_sys]['emergency_num'] = 0
            data_tmp[kfzx][risk_sys]['nonemergency_num'] = 0
    
    #按系统名称维度统计，不在项目中
    sel = select([Risksolution.c.tech_lead_depart,Riskpoint.c.risk_sys,
                  func.count(func.distinct(Risksolution.c.solution_id))],
                and_(Riskpoint.c.point_id == Risksolution.c.riskpoint,
                    Riskpoint.c.in_project == False)
                     ).group_by(Risksolution.c.tech_lead_depart,Riskpoint.c.risk_sys).order_by(Risksolution.c.tech_lead_depart)
    sel_rs = sel.execute()
    for row in sel_rs:
        kfzx,risk_sys,num = row
        if not num or not kfzx:
            continue
        
        if kfzx and (kfzx in data_tmp) and (risk_sys in data_tmp[kfzx]):
#            data_tmp[kfzx] = {}
#            data_tmp[kfzx][risk_sys] = {}
#            data_tmp[kfzx][risk_sys]['kfzx'] = Kfzx.get(kfzx).name
#            data_tmp[kfzx][risk_sys]['risk_sys'] = risk_sys
#            data_tmp[kfzx][risk_sys]['ass_num'] = num
            data_tmp[kfzx][risk_sys]['nonass_num'] = num
            data_tmp[kfzx][risk_sys]['risksolution_num'] += num
#            data_tmp[kfzx][risk_sys]['high-risk_num'] = 0
#            data_tmp[kfzx][risk_sys]['emergency_num'] = 0
#            data_tmp[kfzx][risk_sys]['nonemergency_num'] = 0
        elif kfzx and (kfzx in data_tmp) and not (risk_sys in data_tmp[kfzx]):
            data_tmp[kfzx][risk_sys] = {}
            data_tmp[kfzx][risk_sys]['kfzx'] = Kfzx.get(kfzx).name
            data_tmp[kfzx][risk_sys]['risk_sys'] = risk_sys
            data_tmp[kfzx][risk_sys]['ass_num'] = 0
            data_tmp[kfzx][risk_sys]['nonass_num'] = num
            data_tmp[kfzx][risk_sys]['risksolution_num'] = num
            data_tmp[kfzx][risk_sys]['high-risk_num'] = 0
            data_tmp[kfzx][risk_sys]['emergency_num'] = 0
            data_tmp[kfzx][risk_sys]['nonemergency_num'] = 0
    
    #按系统名称维度统计，高危且紧急
    sel = select([Risksolution.c.tech_lead_depart,Riskpoint.c.risk_sys,
                  func.count(func.distinct(Risksolution.c.solution_id))],
                and_(Riskpoint.c.point_id == Risksolution.c.riskpoint,
                    Riskpoint.c.level== '1',
                    Risksolution.c.urgency.in_(['1','2']))
                     ).group_by(Risksolution.c.tech_lead_depart,Riskpoint.c.risk_sys).order_by(Risksolution.c.tech_lead_depart)
    sel_rs = sel.execute()
    for row in sel_rs:
        kfzx,risk_sys,num = row
        if not num or not kfzx:
            continue
        
        if kfzx and (kfzx in data_tmp) and (risk_sys in data_tmp[kfzx]):    
            data_tmp[kfzx][risk_sys]['high-risk_num'] += num
            data_tmp[kfzx][risk_sys]['emergency_num'] = num
    #按系统名称维度统计，高危非紧急
    sel = select([Risksolution.c.tech_lead_depart,Riskpoint.c.risk_sys,
                  func.count(func.distinct(Risksolution.c.solution_id))],
                and_(Riskpoint.c.point_id == Risksolution.c.riskpoint,
                    Riskpoint.c.level== '1',
                    Risksolution.c.urgency.in_(['3']))
                     ).group_by(Risksolution.c.tech_lead_depart,Riskpoint.c.risk_sys).order_by(Risksolution.c.tech_lead_depart)
    sel_rs = sel.execute()
    for row in sel_rs:
        kfzx,risk_sys,num = row
        if not num or not kfzx:
            continue
        
        if kfzx and (kfzx in data_tmp) and (risk_sys in data_tmp[kfzx]):    
            data_tmp[kfzx][risk_sys]['high-risk_num'] += num
            data_tmp[kfzx][risk_sys]['nonemergency_num'] = num
    
    data = []
    for l,v in data_tmp.items():
        for p,q in v.items():
            data.append(q)
    
    pageno = int(request.values.get('page', 1)) - 1
    rows_per_page = int(request.values.get('rows', settings.get_var('PARA/ROWS_PER_PAGE', 10)))
#    print '+++',data
    #展示        
    view =  SimpleListView(header, data, id='riskpoint_count_table',#table_width=True,
        pagination=True, pageno=pageno,
        rows_per_page=rows_per_page)
    
    if 'download' in request.GET:        
#        fields_convert_map = {'weekreport':weekreport_title}
        return view.download('risksolution_kfzx_system.xls', action='download', timeout=0)
    elif 'data' in request.values:
        return json(view.json())
    else:
        result = view.run(head=True, body=True)
        result['table'] = view
        return result

#管理类报表-风险点执行情况统计
@expose('/charts/riskpoint/current/executing')
def charts_riskpoint_current_executing():
    from uliweb.orm import get_model
    from uliweb.utils.generic import SimpleListView
    from uliweb import request
    from query import create_manage_riskpoint_executing_query
    from uliweb.core.html import Tag
    from decimal import Decimal
    from datetime import date
    
    Risksolution = get_model('risksolution')
    Tasks_Develop = get_model('tasks_develop')
    Requirement = get_model('requirement')
    Riskpoint = get_model('riskpoint')
    Kfzx = get_model('kfzx')
    query = create_manage_riskpoint_executing_query(url_for(charts_riskpoint_current_executing))
    c = query.run()
#    query = None
    condition = None
#    if (request.user.is_gm)and(request.user.department=='2'):
#        c['kfzx'] = request.user._kfzx_
    
    #生成表头
    header = []
    
    if c.get('id_type') and c['id_type'] == '4':
        header.append({'name':'kfzx','verbose_name':u'牵头方', 'width':120})
    elif c.get('id_type') and c['id_type'] == '5':
        header.append({'name':'kfzx','verbose_name':u'准开发需求', 'width':120})
    else:
        header.append({'name':'kfzx','verbose_name':u'开发中心(主责方)', 'width':120})
    header.append({'name':'kfzx_total','verbose_name':u'总计', 'width':80})    
    header.append({'name':'task_wks','verbose_name':u'开发任务方式解决执行状态/未开始', 'width':70})
    header.append({'name':'task_zj','verbose_name':u'开发任务方式解决执行状态/执行中', 'width':70})
    header.append({'name':'task_wc','verbose_name':u'开发任务方式解决执行状态/完成', 'width':70})
    header.append({'name':'non_task','verbose_name':u'开发任务方式解决执行状态/无关联任务', 'width':70})
    header.append({'name':'non_task_wks','verbose_name':u'不需任务方式解决执行状态/未开始', 'width':70})
    header.append({'name':'non_task_zj','verbose_name':u'不需任务方式解决执行状态/执行中', 'width':70})
    header.append({'name':'non_task_wc','verbose_name':u'不需任务方式解决执行状态/完成', 'width':70})
    
#    header.append({'name':'plan','verbose_name':u'计划名称', 'width':100})
#    header.append({'name':'desc','verbose_name':u'完成情况说明', 'width':200})
    
#    sel_weekreport = select([WeekReport.c.title,System.c.abbr,
#                             WeekReport.c.description,],and_())
    data_tmp={}
#    if c['id_type'] == '4':
#        dimension = Risksolution.c.lead_depart
##        sel = select([func.distinct(Risksolution.c.lead_depart)]).group_by(Risksolution.c.lead_depart)
##        sel_rs = sel.execute()
##        for row in sel_rs:
#        for row in Kfzx.filter(and_(Kfzx.c.type == '4')).order_by(Kfzx.c.id):
##            kfzx_id = row[0]
#            kfzx_id = row.id
##            data_tmp[kfzx_id] = kfzx_id
#            data_tmp[kfzx_id] = row.name
#            data_tmp[kfzx_id] = {}
#            data_tmp[kfzx_id]['kfzx'] = row.name
#            data_tmp[kfzx_id]['task_wks'] = 0
#            data_tmp[kfzx_id]['task_zj'] = 0
#            data_tmp[kfzx_id]['task_wc'] = 0
#            data_tmp[kfzx_id]['non_task'] = 0
#            data_tmp[kfzx_id]['kfzx_total'] = 0            
#            data_tmp[kfzx_id]['non_task_wks'] = 0            
#            data_tmp[kfzx_id]['non_task_zj'] = 0            
#            data_tmp[kfzx_id]['non_task_wc'] = 0
#            data_tmp[kfzx_id]['type'] = row.id
    if c['id_type'] == '4':
        dimension = Risksolution.c.lead_depart
#        condition = (Riskpoint.c.point_id == Risksolution.c.riskpoint)
        sel_req = select([func.distinct(Risksolution.c.lead_depart),Kfzx.c.name],and_(Kfzx.c.id == Risksolution.c.lead_depart)).group_by(Risksolution.c.lead_depart).order_by(Risksolution.c.lead_depart)
        rs_sel_req = sel_req.execute()
        for row in rs_sel_req:
            id,title = row
            kfzx_id = id
            data_tmp[kfzx_id] = id
            data_tmp[kfzx_id] = {}
            data_tmp[kfzx_id]['kfzx'] = title
            data_tmp[kfzx_id]['task_wks'] = 0
            data_tmp[kfzx_id]['task_zj'] = 0
            data_tmp[kfzx_id]['task_wc'] = 0
            data_tmp[kfzx_id]['non_task'] = 0
            data_tmp[kfzx_id]['kfzx_total'] = 0
            data_tmp[kfzx_id]['non_task_wks'] = 0
            data_tmp[kfzx_id]['non_task_zj'] = 0
            data_tmp[kfzx_id]['non_task_wc'] = 0
            data_tmp[kfzx_id]['type'] = id    
    elif c['id_type'] == '1':
        dimension = Risksolution.c.tech_lead_depart
        for row in Kfzx.filter(and_(Kfzx.c.type == '1')).order_by(Kfzx.c.id):
            kfzx_id = row.id
            data_tmp[kfzx_id] = row.name
            data_tmp[kfzx_id] = {}
            data_tmp[kfzx_id]['kfzx'] = row.name
            data_tmp[kfzx_id]['task_wks'] = 0
            data_tmp[kfzx_id]['task_zj'] = 0
            data_tmp[kfzx_id]['task_wc'] = 0
            data_tmp[kfzx_id]['non_task'] = 0
            data_tmp[kfzx_id]['kfzx_total'] = 0
            data_tmp[kfzx_id]['non_task_wks'] = 0            
            data_tmp[kfzx_id]['non_task_zj'] = 0            
            data_tmp[kfzx_id]['non_task_wc'] = 0
            data_tmp[kfzx_id]['type'] = row.id
    elif c['id_type'] == '2':
        dimension = Risksolution.c.tech_lead_depart
        for row in Kfzx.filter(and_(Kfzx.c.type == '2')).order_by(Kfzx.c.id):
            kfzx_id = row.id
            data_tmp[kfzx_id] = row.name
            data_tmp[kfzx_id] = {}
            data_tmp[kfzx_id]['kfzx'] = row.name
            data_tmp[kfzx_id]['task_wks'] = 0
            data_tmp[kfzx_id]['task_zj'] = 0
            data_tmp[kfzx_id]['task_wc'] = 0
            data_tmp[kfzx_id]['non_task'] = 0
            data_tmp[kfzx_id]['kfzx_total'] = 0
            data_tmp[kfzx_id]['non_task_wks'] = 0            
            data_tmp[kfzx_id]['non_task_zj'] = 0            
            data_tmp[kfzx_id]['non_task_wc'] = 0
            data_tmp[kfzx_id]['type'] = row.id
    elif c['id_type'] == '3':
        dimension = Risksolution.c.tech_lead_depart
        for row in Kfzx.filter(and_(Kfzx.c.type == '3')).order_by(Kfzx.c.id):
            kfzx_id = row.id
            data_tmp[kfzx_id] = row.name
            data_tmp[kfzx_id] = {}
            data_tmp[kfzx_id]['kfzx'] = row.name
            data_tmp[kfzx_id]['task_wks'] = 0
            data_tmp[kfzx_id]['task_zj'] = 0
            data_tmp[kfzx_id]['task_wc'] = 0
            data_tmp[kfzx_id]['non_task'] = 0
            data_tmp[kfzx_id]['kfzx_total'] = 0
            data_tmp[kfzx_id]['non_task_wks'] = 0
            data_tmp[kfzx_id]['non_task_zj'] = 0
            data_tmp[kfzx_id]['non_task_wc'] = 0
            data_tmp[kfzx_id]['type'] = row.id
    elif c['id_type'] == '5':
        dimension = Riskpoint.c.requirement
        condition = (Riskpoint.c.point_id == Risksolution.c.riskpoint)
        sel_req = select([func.distinct(Riskpoint.c.requirement),Requirement.c.title],and_(Requirement.c.id == Riskpoint.c.requirement)).group_by(Riskpoint.c.requirement).order_by(Riskpoint.c.requirement)
        rs_sel_req = sel_req.execute()
        for row in rs_sel_req:
            id,title = row
            kfzx_id = id
            data_tmp[kfzx_id] = id
            data_tmp[kfzx_id] = {}
            data_tmp[kfzx_id]['kfzx'] = title
            data_tmp[kfzx_id]['task_wks'] = 0
            data_tmp[kfzx_id]['task_zj'] = 0
            data_tmp[kfzx_id]['task_wc'] = 0
            data_tmp[kfzx_id]['non_task'] = 0
            data_tmp[kfzx_id]['kfzx_total'] = 0
            data_tmp[kfzx_id]['non_task_wks'] = 0
            data_tmp[kfzx_id]['non_task_zj'] = 0
            data_tmp[kfzx_id]['non_task_wc'] = 0
            data_tmp[kfzx_id]['type'] = id
    
    #取有对应的任务，且任务处于未开始阶段
    sel = select([dimension,func.count(Risksolution.c.solution_id)],
                and_(Risksolution.c.task_id != None,
                     Risksolution.c.nondev_riskpoint_status == '1',
                     Risksolution.c.solution_mode == '1',
                     condition)).group_by(dimension)
    sel_rs = sel.execute()
    for row in sel_rs:
        kfzx,num = row
#        kfzx = str(kfzx)
        if kfzx and (kfzx in data_tmp):
            data_tmp[kfzx]['task_wks'] = num
    #取有对应的任务，且状态为进行中的整改措施
    sel = select([dimension,func.count(Risksolution.c.solution_id)],
                and_(Risksolution.c.task_id != None,
                     Risksolution.c.nondev_riskpoint_status == '2',
                     Risksolution.c.solution_mode == '1',
                    condition)).group_by(dimension)
    sel_rs = sel.execute()
    for row in sel_rs:
        kfzx,num = row
#        kfzx = str(kfzx)
        if kfzx and (kfzx in data_tmp):
            data_tmp[kfzx]['task_zj'] = num
    #取有对应的任务，且任务处于完成阶段
    sel = select([dimension,func.count(Risksolution.c.solution_id)],
                and_(Risksolution.c.task_id != None,
                     Risksolution.c.nondev_riskpoint_status == 'F',
                     Risksolution.c.solution_mode == '1',
                    condition)).group_by(dimension)
    sel_rs = sel.execute()
    for row in sel_rs:
        kfzx,num = row
#        kfzx = str(kfzx)
        if kfzx and (kfzx in data_tmp):
            data_tmp[kfzx]['task_wc'] = num
    #取有无对应的任务得整改事项统计值
    sel = select([dimension,func.count(Risksolution.c.solution_id)],
                and_(Risksolution.c.task_id == None,
                     Risksolution.c.solution_mode == '1',
                    condition)).group_by(dimension)
    sel_rs = sel.execute()
    for row in sel_rs:
        kfzx,num = row
#        kfzx = str(kfzx)
        if kfzx and (kfzx in data_tmp):
            data_tmp[kfzx]['non_task'] = num

    #取不需任务解决的风险点未开始统计值
    sel = select([dimension,func.count(Risksolution.c.solution_id)],
                and_(Risksolution.c.solution_mode == '2',
                     condition,
                     or_(Risksolution.c.nondev_riskpoint_status == '1',
                        Risksolution.c.nondev_riskpoint_status == None))).group_by(dimension)
    sel_rs = sel.execute()
    for row in sel_rs:
        kfzx,num = row
    #       kfzx = str(kfzx)
        if kfzx and (kfzx in data_tmp):
            data_tmp[kfzx]['non_task_wks'] = num
        
    #取不需任务解决的风险点正在进行统计值
    sel = select([dimension,func.count(Risksolution.c.solution_id)],
                and_(Risksolution.c.solution_mode == '2',
                     Risksolution.c.nondev_riskpoint_status == '2',
                    condition)).group_by(dimension)
    sel_rs = sel.execute()
    for row in sel_rs:
        kfzx,num = row
    #       kfzx = str(kfzx)
        if kfzx and (kfzx in data_tmp):
            data_tmp[kfzx]['non_task_zj'] = num
        
    #取不需任务解决的风险点完成统计值
    sel = select([dimension,func.count(Risksolution.c.solution_id)],
                and_(Risksolution.c.solution_mode == '2',
                     Risksolution.c.nondev_riskpoint_status == 'F',
                    condition)).group_by(dimension)
    sel_rs = sel.execute()
    for row in sel_rs:
        kfzx,num = row
    #       kfzx = str(kfzx)
        if kfzx and (kfzx in data_tmp):
            data_tmp[kfzx]['non_task_wc'] = num
#    print data_tmp
    data = []
    for l,v in data_tmp.items():
        v['kfzx_total'] = v['task_wks'] + v['task_zj'] + v['task_wc'] + v['non_task'] + v['non_task_wks'] + v['non_task_zj'] + v['non_task_wc']
        data.append(v)
#    print '=====',data
#    data.sort(lambda p, q: cmp(p['kfzx_num'], q['kfzx_num']))        
#    print 'vvvvvvvvvv',aaaa    
#    for x,y in sys_data.items():
#        for z,q in y.items():
#            data.append(sys_data)
#    data.sort(lambda p, q: cmp(p['kfzx_num'], q['kfzx_num']))
#    data.reverse()
#    def weekreport_title(value, obj):
##        print 'mmmmm',obj
#        return obj['weekreport_title']
    total_fields={'total_field_name':'合计', 
        'fields':['kfzx_total',
                'task_wks',
                'task_zj',
                'task_wc',
                'non_task',
                'non_task_wks',
                'non_task_zj',
                'non_task_wc']}
    def task_wks(value, obj):
        if c['id_type'] == '4':
            return str(Tag('a', value, href=url_for('risks.views.RiskSolutionView.list', 
                        task_stage='1',lead_depart=obj['type']),target='_blank'))
        if c['id_type'] == '1' or c['id_type'] == '2' or c['id_type'] == '3':
            return str(Tag('a', value, href=url_for('risks.views.RiskSolutionView.list', 
                        task_stage='1',tech_lead_depart=obj['type']),target='_blank'))
        
    def task_zj(value, obj):
        if c['id_type'] == '4':
            return str(Tag('a', value, href=url_for('risks.views.RiskSolutionView.list', 
                        task_stage='2',lead_depart=obj['type']),target='_blank'))
        if c['id_type'] == '1' or c['id_type'] == '2' or c['id_type'] == '3':
            return str(Tag('a', value, href=url_for('risks.views.RiskSolutionView.list', 
                        task_stage='2',tech_lead_depart=obj['type']),target='_blank'))
    
    def task_wc(value, obj):
        if c['id_type'] == '4':
            return str(Tag('a', value, href=url_for('risks.views.RiskSolutionView.list', 
                        task_stage='F',lead_depart=obj['type']),target='_blank'))
        if c['id_type'] == '1' or c['id_type'] == '2' or c['id_type'] == '3':
            return str(Tag('a', value, href=url_for('risks.views.RiskSolutionView.list', 
                        task_stage='F',tech_lead_depart=obj['type']),target='_blank'))
    def non_task(value, obj):
        if c['id_type'] == '4':
            return str(Tag('a', value, href=url_for('risks.views.RiskSolutionView.list', 
                        solution_mode='1',nondev_riskpoint_status='N',lead_depart=obj['type']),target='_blank'))
        if c['id_type'] == '1' or c['id_type'] == '2' or c['id_type'] == '3':
            return str(Tag('a', value, href=url_for('risks.views.RiskSolutionView.list', 
                        solution_mode='1',nondev_riskpoint_status='N',tech_lead_depart=obj['type']),target='_blank'))
    
    def kfzx_total(value, obj):
        if c['id_type'] == '4':
            return str(Tag('a', value, href=url_for('risks.views.RiskSolutionView.list', 
                        lead_depart=obj['type']),target='_blank'))
        if c['id_type'] == '1' or c['id_type'] == '2' or c['id_type'] == '3':
            return str(Tag('a', value, href=url_for('risks.views.RiskSolutionView.list', 
                        tech_lead_depart=obj['type']),target='_blank'))
    def non_task_wks(value, obj):
        if c['id_type'] == '4':
            return str(Tag('a', value, href=url_for('risks.views.RiskSolutionView.list', 
                        nondev_riskpoint_status='1',lead_depart=obj['type']),target='_blank'))
        if c['id_type'] == '1' or c['id_type'] == '2' or c['id_type'] == '3':
            return str(Tag('a', value, href=url_for('risks.views.RiskSolutionView.list', 
                        nondev_riskpoint_status='1',tech_lead_depart=obj['type']),target='_blank'))
    def non_task_zj(value, obj):
        if c['id_type'] == '4':
            return str(Tag('a', value, href=url_for('risks.views.RiskSolutionView.list', 
                        nondev_riskpoint_status='2',lead_depart=obj['type']),target='_blank'))
        if c['id_type'] == '1' or c['id_type'] == '2' or c['id_type'] == '3':
            return str(Tag('a', value, href=url_for('risks.views.RiskSolutionView.list', 
                        nondev_riskpoint_status='2',tech_lead_depart=obj['type']),target='_blank'))
    def non_task_wc(value, obj):
        if c['id_type'] == '4':
            return str(Tag('a', value, href=url_for('risks.views.RiskSolutionView.list', 
                        nondev_riskpoint_status='F',lead_depart=obj['type']),target='_blank'))
        elif c['id_type'] == '1' or c['id_type'] == '2' or c['id_type'] == '3':
            return str(Tag('a', value, href=url_for('risks.views.RiskSolutionView.list', 
                        nondev_riskpoint_status='F',tech_lead_depart=obj['type']),target='_blank'))
    if c.get('id_type') and c['id_type'] == '5':
        fields_convert_map = {}
    else:
        fields_convert_map = {'task_wks':task_wks,'task_zj':task_zj,'task_wc':task_wc,'non_task':non_task,'kfzx_total':kfzx_total,'non_task_wks':non_task_wks,'non_task_zj':non_task_zj,'non_task_wc':non_task_wc}
    pageno = int(request.values.get('page', 1)) - 1
    rows_per_page = int(request.values.get('rows', settings.get_var('PARA/ROWS_PER_PAGE', 10)))
#    print '+++',data
    #展示        
    view =  SimpleListView(header, data, id='riskpoint_count_table',#table_width=True,
        pagination=True, pageno=pageno,total_fields=total_fields,fields_convert_map=fields_convert_map,
        rows_per_page=rows_per_page)
    
    if 'download' in request.GET:        
#        fields_convert_map = {'weekreport':weekreport_title}
        return view.download('riskpoint_current_executing.xls', action='download', timeout=0)
    elif 'data' in request.values:
        return json(view.json())
    else:
        result = view.run(head=True, body=True)
        result['table'] = view
        result.update({'query_form':query.form})
        return result
